clear all

set seed 249233
local bsiter = 1000
 


cd "/Users/sambuehl/Dropbox/Demand_for_Information/DATA_ANALYSIS/"

* make datasets lean



set more off

use "data/data_all.dta", replace

drop if session == 1

merge m:1 subj using "data/alphas.dta"
drop _merge

gen logit_p11 = log(p11/(1-p11))
gen logit_p00 = log(p00/(1-p00))
gen loglik11 = log(a/(1-b))
gen loglik00 = log(b/(1-a))

drop if info_str == 11

save "data/pool.dta", replace






postfile across_valuations id b_cons se_cons b_diff_emp se_diff_emp b_diff_sim se_diff_sim r2 obs equation using "data/across_valuations_all_bs.dta", replace





****** start bootstrapping here

forvalues i = 1/`bsiter' {


	use "data/pool.dta", replace

	keep if tag_sub == 1
	keep sub id alpha se_alpha


	gen id_new = _n

	expandby 10, by(id_new)
	sort id_new
	gen info_structure = _n
	replace info_structure = info_structure - 10*floor(info_structure/10)
	replace info_structure = 10 if info_structure == 0

	qui merge m:1 subject info_struc using "data/pool.dta"
	drop if _merge == 2
	drop _merge



	replace alpha = . if tag_sub == 0
	gen alpha_rnd = rnormal(alpha, se_alpha)
	drop alpha
	sort subj
	by subj: egen alpha = mean(alpha_rnd)

	gen pred_logit_p11 = alpha * loglik11
	gen pred_logit_p00 = alpha * loglik00



	bsample, cluster(subject) idcluster(id)


	* estimation 

	gen pred_p11 = exp(pred_logit_p11)/(1 + exp(pred_logit_p11))
	gen pred_p00 = exp(pred_logit_p00)/(1 + exp(pred_logit_p00))
	replace pred_p00 = 1 if pred_p00 == .
	gen pred_v_pred = b_p1 * pred_p11 + (1 - b_p1) * pred_p00




	******* averaged over information structures

	gen diff = v - v_pred_true_all
	gen diff_sim = pred_v_pred - v_pred_true_all
	gen diff_emp = v_pred - v_pred_true_all





	sort id
	by id: egen mean_diff = mean(diff)
	by id: egen mean_diff_sim = mean(diff_sim)
	by id: egen mean_diff_emp = mean(diff_emp)


	xi: pcorr mean_diff mean_diff_emp i.session i.group 


	reg mean_diff mean_diff_emp if tag_sub == 1
	post across_valuations (`i') (_b[_cons]) (_se[_cons]) (_b[mean_diff_emp]) (_se[mean_diff_emp]) (.) (.) (e(r2)) (e(N)) (0)


	reg diff diff_emp 
	post across_valuations (`i') (_b[_cons]) (_se[_cons]) (_b[diff_emp]) (_se[diff_emp]) (.) (.) (e(r2)) (e(N)) (1)
	reg diff diff_sim 
	post across_valuations (`i') (_b[_cons]) (_se[_cons]) (.) (.) (_b[diff_sim]) (_se[diff_sim]) (e(r2)) (e(N)) (2)
	reg diff diff_emp diff_sim 
	post across_valuations (`i') (_b[_cons]) (_se[_cons]) (_b[diff_emp]) (_se[diff_emp]) (_b[diff_sim]) (_se[diff_sim]) (e(r2)) (e(N)) (3)


	capture: gen extreme = 0
	su alpha, d
	replace extreme = 0
	replace extreme = 1 if alpha < r(p10) + 0.00001
	replace extreme = 1 if alpha > r(p90) - 0.00001

	reg diff diff_emp if  extreme == 0
	post across_valuations (`i') (_b[_cons]) (_se[_cons]) (_b[diff_emp]) (_se[diff_emp]) (.) (.) (e(r2)) (e(N)) (4)
	reg diff diff_sim  if extreme == 0
	post across_valuations (`i') (_b[_cons]) (_se[_cons]) (.) (.) (_b[diff_sim]) (_se[diff_sim]) (e(r2)) (e(N)) (5)
	reg diff diff_emp diff_sim if extreme == 0
	post across_valuations (`i') (_b[_cons]) (_se[_cons]) (_b[diff_emp]) (_se[diff_emp]) (_b[diff_sim]) (_se[diff_sim]) (e(r2)) (e(N)) (6)

	
	
	areg diff diff_emp, a(info_str)
	post across_valuations (`i') (_b[_cons]) (_se[_cons]) (_b[diff_emp]) (_se[diff_emp]) (.) (.) (e(r2)) (e(N)) (7)
	areg diff diff_sim, a(info_str)
	post across_valuations (`i') (_b[_cons]) (_se[_cons]) (.) (.) (_b[diff_sim]) (_se[diff_sim]) (e(r2)) (e(N)) (8)
	areg diff diff_emp diff_sim, a(info_str)
	post across_valuations (`i') (_b[_cons]) (_se[_cons]) (_b[diff_emp]) (_se[diff_emp]) (_b[diff_sim]) (_se[diff_sim]) (e(r2)) (e(N)) (9)

	

}

postclose across_valuations

clear

use "data/across_valuations_all_bs.dta", replace


su b_cons se_cons b_diff_emp se_diff_emp b_diff_sim se_diff_sim r2 obs if equation == 1
su b_cons se_cons b_diff_emp se_diff_emp b_diff_sim se_diff_sim r2 obs if equation == 2
su b_cons se_cons b_diff_emp se_diff_emp b_diff_sim se_diff_sim r2 obs if equation == 3
su b_cons se_cons b_diff_emp se_diff_emp b_diff_sim se_diff_sim r2 obs if equation == 4
su b_cons se_cons b_diff_emp se_diff_emp b_diff_sim se_diff_sim r2 obs if equation == 5
su b_cons se_cons b_diff_emp se_diff_emp b_diff_sim se_diff_sim r2 obs if equation == 6
su b_cons se_cons b_diff_emp se_diff_emp b_diff_sim se_diff_sim r2 obs if equation == 7
su b_cons se_cons b_diff_emp se_diff_emp b_diff_sim se_diff_sim r2 obs if equation == 8
su b_cons se_cons b_diff_emp se_diff_emp b_diff_sim se_diff_sim r2 obs if equation == 9

